package ktmall.dao.impl;

import com.ktjiaoyu.ktmall.dao.BaseDao;
import com.ktjiaoyu.ktmall.dao.CategoryDao;
import com.ktjiaoyu.ktmall.entity.AdminUser;
import com.ktjiaoyu.ktmall.entity.Category;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {

    @Override
    public List<Category> findAll() {
        String sql = "select * from kt_category where is_delete=0 order by parent_id, sort";
        try {
            return executeQuery(sql, resultSet -> {
                List<Category> list = new ArrayList<Category>();
                try {
                    while (resultSet.next()) {
//                        Category category = new Category(1111);
//                        category.setId(resultSet.getLong("id"));
//                        category.setName(resultSet.getString("name"));
//                        category.setParent_id(resultSet.getLong("parent_id"));
//                        category.setIcon(resultSet.getString("icon"));
//                        category.setSort(resultSet.getInt("sort"));
//                        category.setStatus(resultSet.getInt("status"));
//                        category.setIs_deleted(resultSet.getInt("is_deleted"));
//                        category.setUpdate_time(resultSet.getString("update_time"));
//                        list.add(category);
                        list.add(extracCategory(resultSet));
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                } finally {
                }
                return list;
            });
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        } finally {
        }
    }

    private Category extracCategory(ResultSet resultSet) throws SQLException {
//        List<Category> list = new ArrayList<>();
        Category category = new Category();
        category.setId(resultSet.getLong("id"));
        category.setName(resultSet.getString("name"));
        category.setParent_id(resultSet.getLong("parent_id"));
        category.setIcon(resultSet.getString("icon"));
        category.setSort(resultSet.getInt("sort"));
        category.setStatus(resultSet.getInt("status"));
        category.setIs_deleted(resultSet.getInt("is_deleted"));
        category.setUpdate_time(resultSet.getString("update_time"));
//        category.setChildren(list.add(category));
        return category;
    }
}
